package com.heyige.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heyige.entity.MemberOrder;
import com.heyige.mapper.MemberOrderMapper;
import com.heyige.service.MemberOrderService;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service("memberOrderService")
public class MemberOrderServiceImpl extends ServiceImpl<MemberOrderMapper, MemberOrder> implements MemberOrderService {
    
    private final MemberOrderMapper memberOrderMapper;
    
    public MemberOrderServiceImpl(MemberOrderMapper memberOrderMapper) {
        this.memberOrderMapper = memberOrderMapper;
    }
    
    @Override
    public Optional<MemberOrder> findByIdAndUserId(Long id, Long userId) {
        LambdaQueryWrapper<MemberOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberOrder::getId, id)
                .eq(MemberOrder::getUserId, userId);
        return Optional.ofNullable(memberOrderMapper.selectOne(queryWrapper));
    }
    
    @Override
    public Page<MemberOrder> findByUserIdOrderByCreatedAtDesc(Long userId, Page<MemberOrder> page) {
        LambdaQueryWrapper<MemberOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberOrder::getUserId, userId)
                .orderByDesc(MemberOrder::getCreatedAt);
        return memberOrderMapper.selectPage(page, queryWrapper);
    }
}